
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_widget/code7/provider/demo1/provider_mult.dart';
import 'package:flutter_base_widget/code7/provider/demo1/time_counter_model.dart';
import 'package:provider/provider.dart';
import 'package:intl/intl.dart';

import 'consumer_page.dart';
import 'provider_base.dart';
void main() => runApp(
  ChangeNotifierProvider(
    create: (BuildContext context) {
      return TimeCounterModel();
    },
    child: const MaterialApp(
      ///默认的首页面
      home: ProviderHomePage(),
    ),
  ),
);
class ProviderHomePage extends StatefulWidget {
  const ProviderHomePage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return _ProviderHomePageState();
  }
}

class _ProviderHomePageState extends State {
  final List<Map<String, dynamic>> itemList = [
    {"name": "Provider 接收数据", "widget": TestProviderTimePage()},
    {"name": "Consumer 接收数据", "widget": ConsumerTimePage()},
    {"name": "多个页面传值", "widget": TestProviderMulPage()},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Provider 专题 "),
      ),
      body: SizedBox(
        width: double.infinity,
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return itemBuildWidget(index);
          },
          itemCount: itemList.length,
        ),
      ),
    );
  }

  Widget itemBuildWidget(int index) {
    Map<String, dynamic> item = itemList[index];
    String buttonTitle = item["name"];
    Widget toPage = item["widget"];

    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10),
      child: OutlineButton(
        child: Text(buttonTitle),
        onPressed: () {
          Navigator.of(context)
              .push(MaterialPageRoute(builder: (BuildContext context) {
            return toPage;
          }));
        },
      ),
    );
  }
}